﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PhoneManager
{
    public partial class UCPhoneBook : UserControl
    {
        public delegate void UCPhoneBookEventHandler(object sender, UCPhoneBookEventArgs e);
        public event UCPhoneBookEventHandler EventUCPhoneBook;

        public UCPhoneBook()
        {
            InitializeComponent();
        }
        public int Index { get; set; }


        public void ChangeNameContact (string NewName)
        {
            this.TxtName.Text = NewName;
        }
        
        public string NameContact
        { 
            get { return this.TxtName.Text;}
            set {
                value = value.Trim();
                if (value.Length > TxtName.MaxLength)
                {
                    value = value.Substring(0, TxtName.MaxLength);
                }
                this.TxtName.Tag = value; 
                this.TxtName.Text = value;
                if (RedColor)
                {
                    TxtName.ForeColor = Color.Red;
                }
            }
        }
        public string NumberContact 
        { 
            get { return this.TxtNumber.Text;}
            set {
                value = value.Trim();
                if (value.Length > TxtNumber.MaxLength)
                {
                    value = value.Substring(0, TxtNumber.MaxLength);
                }
                this.TxtNumber.Tag = value; 
                this.TxtNumber.Text = value;
                if (RedColor)
                {
                    TxtName.ForeColor = Color.Red;
                }
            }
        }

        public bool IsHasChange()
        {
            bool ret = false;
            if (ChkAddP.Checked != Boolean.Parse(ChkAddP.Tag.ToString()))
            {
                ret = true;
            }
            if (ChkAddS.Checked != Boolean.Parse(ChkAddS.Tag.ToString()))
            {
                ret = true;
            }
            if (TxtName.Text != TxtName.Tag.ToString())
            {
                ret = true;
            }
            if (TxtNumber.Text != TxtNumber.Tag.ToString())
            {
                ret = true;
            }
            return ret;
        }

        public bool SentToPhone
        {
            get { return this.ChkAddP.Checked; }
            set 
            {
                this.ChkAddP.Checked = value;
                this.ChkAddP.Tag = value.ToString();
                if (!value && !ChkAddS.Checked)
                {
                    this.ChkDel.Checked = true;
                }
                else
                {
                    this.ChkDel.Checked = false;
                }
                if (value)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                }
                else
                {
                    if (SendToSIM)
                    {
                        TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                        TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                    }
                    else
                    {
                        TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                        TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                    }
                }
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }
        public bool SendToSIM
        {
            get { return this.ChkAddS.Checked; }
            set
            {
                this.ChkAddS.Checked = value;
                this.ChkAddS.Tag = value.ToString();
                if (!value && !ChkAddP.Checked)
                {
                    this.ChkDel.Checked = true;
                }
                else
                {
                    this.ChkDel.Checked = false;
                }
                if (value)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                }
                else
                {
                    if (SentToPhone)
                    {
                        TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                        TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                    }
                    else
                    {
                        TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                        TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                    }
                }
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }
        public bool IsRemoveEnabled
        {
            get { return this.ChkDel.Enabled; }
            set { this.ChkDel.Enabled = value; }
        }
        public string Encoding { get; set; }
        public string PhoneID { get; set; }
        public string SIMID { get; set; }
        public int RestoreIndex { get; set; }
        public bool IsRemove
        { 
            get { return ChkDel.Checked; } 
        }
        private bool _RedColor = false; 
        public bool RedColor 
        {
            get { return _RedColor ; }
            set 
            {
                _RedColor = value;
                if (value)
                {
                    TxtName.ForeColor = Color.Red;
                    TxtNumber.ForeColor = Color.Red;
                }
                else
                {
                    TxtName.ForeColor = SystemColors.WindowText;
                    TxtNumber.ForeColor = SystemColors.WindowText;
                }
            } 
        }
        private void ChkAddP_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkAddP.Checked)
            {
                ChkDel.Checked = false;
            }
            if (!ChkAddP.Checked && !ChkAddS.Checked)
            {
                this.ChkDel.Checked = true;
            }
            else
            {
                this.ChkDel.Checked = false;
            }
            if (ChkAddP.Checked)
            {
                TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
            }
            else
            {
                if (ChkAddS.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
            if (EventUCPhoneBook != null)
            {
                EventUCPhoneBook(this, new UCPhoneBookEventArgs(ChkAddP.Checked ? 1 : -1,0));
            }
        }
        private void ChkAddS_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkAddS.Checked)
            {
                ChkDel.Checked = false;
            }
            if (!ChkAddP.Checked && !ChkAddS.Checked)
            {
                this.ChkDel.Checked = true;
            }
            else
            {
                this.ChkDel.Checked = false;
            }
            if (ChkAddS.Checked)
            {
                TxtName.MaxLength = SharedLib.PhoneBookSIMNameMaxLength;
                TxtNumber.MaxLength = SharedLib.PhoneBookSIMNumberMaxLength;
            }
            else
            {
                if (ChkAddP.Checked)
                {
                    TxtName.MaxLength = SharedLib.PhoneBookMEMNameMaxLength;
                    TxtNumber.MaxLength = SharedLib.PhoneBookMEMNumberMaxLength;
                }
                else
                {
                    TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                    TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
                }
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
            if (EventUCPhoneBook != null)
            {
                EventUCPhoneBook(this, new UCPhoneBookEventArgs(0, ChkAddS.Checked?1:-1));
            }
        }

        private void TxtName_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text.Trim() == "")
            {
                ChkAddP.Enabled = false;
                ChkAddS.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            }
            else
            {
                ChkAddP.Enabled = true;
                ChkAddS.Enabled = true;
            }
            if (TxtNumber.Text.Trim() == "")
            {
                ChkAddP.Enabled = false;
                ChkAddS.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            }
            else
            {
                ChkAddP.Enabled = true;
                ChkAddS.Enabled = true;
            }
        }

        private void TxtNumber_TextChanged(object sender, EventArgs e)
        {
            if (TxtName.Text.Trim() == "")
            {
                ChkAddP.Enabled = false;
                ChkAddS.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            }
            else
            {
                ChkAddP.Enabled = true;
                ChkAddS.Enabled = true;
            }
            if (TxtNumber.Text.Trim() == "")
            {
                ChkAddP.Enabled = false;
                ChkAddS.Enabled = false;
                ChkAddP.Checked = false;
                ChkAddS.Checked = false;
                TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            }
            else
            {
                ChkAddP.Enabled = true;
                ChkAddS.Enabled = true;
            }
        }

        private void TxtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\b')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '#')
            {
                e.Handled = false;
                return;
            }
            if (e.KeyChar == '*')
            {
                e.Handled = false;
                return;
            }
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
        }

        private void TxtNumber_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtNumber.Text = TxtNumber.Tag.ToString();
                if (TxtNumber.Text.Length > TxtNumber.MaxLength)
                {
                    TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
                }
            }
        }

        private void TxtName_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 27)
            {
                TxtName.Text = TxtName.Tag.ToString();
                if (TxtName.Text.Length > TxtName.MaxLength)
                {
                    TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
                }
            }
        }

        private void UCPhoneBook_Load(object sender, EventArgs e)
        {
            TxtName.MaxLength = Math.Max(SharedLib.PhoneBookMEMNameMaxLength, SharedLib.PhoneBookSIMNameMaxLength);
            TxtNumber.MaxLength = Math.Max(SharedLib.PhoneBookMEMNumberMaxLength, SharedLib.PhoneBookSIMNumberMaxLength);
            if (ChkAddS.Checked)
            {
                TxtName.MaxLength = Math.Min(TxtName.MaxLength, SharedLib.PhoneBookSIMNameMaxLength);
                TxtNumber.MaxLength = Math.Min(TxtNumber.MaxLength, SharedLib.PhoneBookSIMNameMaxLength); ;
            }
            if (ChkAddP.Checked)
            {
                TxtName.MaxLength = Math.Min(TxtName.MaxLength, SharedLib.PhoneBookMEMNameMaxLength);
                TxtNumber.MaxLength = Math.Min(TxtNumber.MaxLength, SharedLib.PhoneBookMEMNameMaxLength); ;
            }
            if (TxtName.Text.Length > TxtName.MaxLength)
            {
                TxtName.Text = TxtName.Text.Substring(0, TxtName.MaxLength);
            }
            if (TxtNumber.Text.Length > TxtNumber.MaxLength)
            {
                TxtNumber.Text = TxtNumber.Text.Substring(0, TxtNumber.MaxLength);
            }
        }

    }
    public class UCPhoneBookEventArgs : EventArgs
    {
        private int _CheckedPhone;
        private int _CheckedSim;
        public UCPhoneBookEventArgs(int Phone, int Sim)
        {
            _CheckedPhone = Phone;
            _CheckedSim = Sim;

        }
        public int CheckedPhone { get { return _CheckedPhone; } }
        public int CheckedSim { get { return _CheckedSim; } }
    }
}
